﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 台签打印
{
    public class RectangleArranger
    {
        struct Arrange
        {
            public int Rows { get; set; }
            public int Cols { get; set; }
            public override string ToString() => $"{{ Rows={Rows}, Cols={Cols} }}";

        }

        /// <summary>
        /// 计算矩形排列方案
        /// </summary>
        /// <param name="large">大矩形尺寸</param>
        /// <param name="small">小矩形尺寸</param>
        /// <param name="Landscape">方向</param>
        /// <returns>返回所有小矩形的左上角坐标列表</returns>
        public static List<Rectangle> Calculate(Size large, Size small, ref bool? Landscape)
        {
            // 检验输入
            if (small.Width <= 0 || small.Height <= 0) throw new ArgumentException("矩形尺寸必须大于0");
            //if (large.Width < small.Width || large.Height < small.Height) throw new ArgumentException("矩形尺寸不能超过大");

            // 计算两种排列方案
            var horizontal = CalculateArrangement(large, small);
            var vertical = CalculateArrangement(new Size(large.Height, large.Width), small);

            // 选择方向
            if (!Landscape.HasValue)
            {
                Landscape = (horizontal.Cols * horizontal.Rows < vertical.Cols * vertical.Rows);
            }

            // 尺寸过大返回
            if (horizontal.Cols < 1) horizontal.Cols = 1;
            if (horizontal.Rows < 1) horizontal.Rows = 1;
            if (vertical.Cols < 1) vertical.Cols = 1; 
            if (vertical.Rows < 1) vertical.Rows = 1;

            // 调试信息
            Console.WriteLine($"horizontal={horizontal}");
            Console.WriteLine($"vertical={vertical}");
            Console.WriteLine($"Landscape={Landscape}");

            return Landscape.Value
                ? GeneratePositions(new Size(large.Height, large.Width), small, vertical.Cols, vertical.Rows)
                : GeneratePositions(large, small, horizontal.Cols, horizontal.Rows);

        }

        // 排列计算
        private static Arrange CalculateArrangement(Size large, Size small)
        {
            int maxCols = (int)(large.Width / small.Width);
            int maxRows = (int)(large.Height / small.Height);
            return new Arrange { Cols = maxCols, Rows = maxRows };
        }

        // 生成坐标列表
        private static List<Rectangle> GeneratePositions(Size large, Size small, int cols, int rows)
        {
            var rectangles = new List<Rectangle>();

            // 计算总占用空间
            int totalWidth = cols * small.Width;
            int totalHeight = rows * small.Height;

            // 计算居中偏移量
            int offsetX = (large.Width - totalWidth) / 2;
            int offsetY = (large.Height - totalHeight) / 2;

            // 生成每个位置
            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    rectangles.Add(new Rectangle
                    {
                        X = offsetX + col * small.Width,
                        Y = offsetY + row * small.Height,
                        Width = small.Width,
                        Height = small.Height,
                    });
                }
            }
            return rectangles;
        }

    }




}
